package com.hwhhhh.mygrad;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hwhhhh.mygrad.utils.StringUtils;

import java.util.HashMap;

// 此处设置的展示用recyclerview展示，待改进
// 暂时废弃。记录ItemDecoration的使用。
public class SettingActivity extends BaseActivity {
    private static final String[] SETS = new String[] {
            "账号资料", "教务设置", "推送设置", "消息设置", "清理存储空间", "管理员功能", "问题反馈", "用户协议", "隐私政策",  "隐私权限设置"
    };
    private static final String FOOTER = "退出登录";
    private RecyclerView rvSetting;

    @Override
    protected void initView() {
        rvSetting = findViewById(R.id.rv_setting);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initData() {
        SettingAdapter adapter = new SettingAdapter(SETS, FOOTER);
        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(0, 8);
        map.put(2, 8);
        map.put(5, 8);
        map.put(6, 8);
        map.put(7, 8);
        SettingItemDecoration decoration = new SettingItemDecoration(this, map);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rvSetting.setAdapter(adapter);
        rvSetting.addItemDecoration(decoration);// 需要在LayoutManager之前
        rvSetting.setLayoutManager(linearLayoutManager);
    }

    class SettingAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private static final int TYPE_NORMAL = 0;
        private static final int TYPE_FOOTER = 1;
        private String[] sets;
        private String footer;

        public SettingAdapter(String[] sets, String footer) {
            this.sets = sets;
            this.footer = footer;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            if (viewType == TYPE_NORMAL) return new SettingHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_setting, parent, false));
            else if (viewType == TYPE_FOOTER) return new SettingFooterHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_setting_footer, parent, false));
            return null;
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof SettingHolder) {
                ((SettingHolder) holder).tvName.setText(sets[position]);
            } else if (holder instanceof SettingFooterHolder) {
                ((SettingFooterHolder) holder).tvName.setText(footer);
            }
        }

        @Override
        public int getItemViewType(int position) {
            if (getItemCount() - 1 == position) return TYPE_FOOTER;
            return TYPE_NORMAL;
        }

        @Override
        public int getItemCount() {
            return sets.length + 1;
        }
    }

    class SettingHolder extends RecyclerView.ViewHolder {
        private TextView tvName;
        private View rootView;

        public SettingHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_setting_name);
            rootView = itemView;
        }
    }

    class SettingFooterHolder extends RecyclerView.ViewHolder {
        private TextView tvName;
        private View rootView;

        public SettingFooterHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_setting_footer_name);
            rootView = itemView;
        }
    }

    class SettingItemDecoration extends RecyclerView.ItemDecoration {
        private HashMap<Integer, Integer> dividePos;    // 分割位置以及分割的宽度
        private Context mContext;
        private int bigDivHeight, smallDivHeight;

        // 这里传递的hashmap为 <分割位置，分割宽度> 但这里没有用到分割宽度。待改进
        SettingItemDecoration(Context context, HashMap<Integer, Integer> dividePos) {
            this.mContext = context;
            this.dividePos = dividePos;
            this.bigDivHeight = StringUtils.dpToInt(mContext, 16);
            this.smallDivHeight = StringUtils.dpToInt(mContext, 1);
        }

        @Override
        public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.onDraw(c, parent, state);
            Drawable bigDraw = new ColorDrawable(getResources().getColor(R.color.divider_color_1));
            Drawable smallDraw = new ColorDrawable(getResources().getColor(R.color.divider_color));
            int left = parent.getPaddingLeft();
            int right = parent.getWidth() - parent.getPaddingRight();
            int top, bottom;
            for (int i = 0; i < parent.getChildCount(); i ++) {
                View child = parent.getChildAt(i);
                RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
                if (!dividePos.containsKey(i)) {
                    top = child.getTop() - params.topMargin - smallDivHeight;
                    bottom = top + smallDivHeight;
                    smallDraw.setBounds(left, top, right, bottom);
                    smallDraw.draw(c);
                } else {
                    top = child.getTop() - params.topMargin - bigDivHeight;
                    bottom = top + bigDivHeight;
                    bigDraw.setBounds(left, top, right, bottom);
                    bigDraw.draw(c);
                }
            }
        }

        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            int pos = parent.getChildAdapterPosition(view);
            if (isDividePos(pos)) {
                outRect.top = bigDivHeight;
                outRect.left = 0;
                outRect.right = 0;
                outRect.bottom = 0;
            } else {    // 普通分割线
                outRect.top = smallDivHeight;
                outRect.left = 0;
                outRect.right = 0;
                outRect.bottom = 0;
            }
        }

        private boolean isDividePos(int pos) {
            return dividePos.containsKey(pos);
        }
    }
}